﻿using ACC.Business;
using ACC.Business.Process;
using ACC.Data.ACCLog;
using ACC.Data.Comm;
using ACC.Data.Entities.Common;
using ACC.Data.Entities.Mes;
using ACC.Data.Line;
using ACC.Device;
using ACC.Device.PLC;
using ACC.Interface.Extensions;
using ACC.LineDevice.KukaPack;
using ACC.Util.Restful;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Script
{
    /// <summary>
    /// 模拟下线点站
    /// </summary>
    public class Stn30 : StnProcessKukaPack
    {
        public Stn30(StnRunningData data)
            : base(data)
        {
            // 产品异常下线点
            IsDownCell = true;
        }

        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB190_Offset = 200;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = 0;

                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = 234;
                kukaSiemensDev.DB193_ACC_Offset = 268;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = 234;
                kukaSiemensDev.DB194_PLC_Data_Offset = 478;
                kukaSiemensDev.DB197 = 90;
                kukaSiemensDev.DB197_PLC_Offset = 0;

                kukaSiemensDev.BarcodeLength = 30;
                kukaSiemensDev.LoadBarcodeCount = 1;
                kukaSiemensDev.UnloadBarcodeCount = 1;

                kukaSiemensDev.PartTypeDataIntLength = 20;
                kukaSiemensDev.PartTypeDataRealLength = 50;
                kukaSiemensDev.PartTypeDataStringLength = 5;
                kukaSiemensDev.PartTypeDataStringDataLength = 30;
                kukaSiemensDev.ProcessDataStringDataLength = 30;
            }

            return dev;
        }

        public override ACCCode ReadLoadParam(ACCDevice dev, out ACCDevice.LoadParam param)
        {
            ACCCode rs = base.ReadLoadParam(dev, out param);
            if (rs == ACCCode.ACCOK)
            {
                // 如果获取的新件的型号与当前型号不一致，可以调用该方法通知PLC换型，同时该方法也进行ACC的换型
                //ChangePartNo(_data.PartNo, true);
            }

            return rs;
        }

        public override ACCDevice.PrintResult ProcessPrintLabel(ACCDevice dev)
        {
            // 调用WebApi 获取下一个可用的SN及类型
            var printResult = new ACCDevice.PrintResult()
            {
                ErrorCode = ACCCode.ACCOK,
                ReturnCode = 0,
                ErrorMessage = string.Empty
            };

            var param = new Dictionary<string, string>();
            param.Add("line", _lineData.Line);
            param.Add("op", _data.StnCfg.OPCfg.OP);
            param.Add("stn", _data.StnCfg.Name);
            param.Add("unitsn", "A1234324245");
            param.Add("sn_type", "2");

            var result = PostWebApiMessage("GetSerialNumber", null, param);

            // TO-DO: only for test
            //result.IsSuccess = true;
            //result.Content = "A1234324245";
            //result.Msg = "PN001";

            string sn = string.Empty;
            if (result.IsSuccess && result.Content != null)
            {
                var cont = JsonHelper.ToObject<MESSerialNumberSync>(result.Content.ToString());
                sn = cont == null ? "" : cont.gb_sn;
                if(!string.IsNullOrEmpty(sn) && _data.PartNo != null)
                {
                    PrinterProcess.DoPrint("LabelPrinter", _data.PartNo.BomCfg, "LabelCell", logger);
                }
                else
                {
                    printResult.ErrorCode = ACCCode.ACCNotStartPartNo;
                }
            }

            int retCode = 0;
            string retMsg = string.Empty;
            ProcessReturnCodeAndErrorMsg(ref retCode, ref retMsg, 0, printResult.ErrorCode, null);
            StnEventLog.Log(_data, printResult.ErrorCode == ACCCode.ACCOK ? StnLog.LogType.Success : StnLog.LogType.Error,
                string.Format("条码打印 UnitSN={0} {1}, ReturnCode={2}, ReturnMsg={3}", sn, retCode == 0 ? "成功" : "失败", retCode, retMsg));
            printResult.ErrorCode = (ACCCode)retCode;

            return printResult;
        }
    }
}
